import config from '@/config/config.js'
import request from '@/utils/request'

// 通用上传请求
export function upload(filePath) {
  console.log(filePath)
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: config.baseUrl + config.apiPrefix + '/common/upload',
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync('token')
      },
      success: (uploadRes) => {
        try {
          const res = JSON.parse(uploadRes.data)
          resolve(res)
        } catch (e) {
          reject(new Error('上传失败'))
        }
      },
      fail: () => {
        reject(new Error('上传失败'))
      }
    })
  })
}

// 批量上传图片，循环调用单文件上传
export function uploadBatch(filePaths) {
  return Promise.all(filePaths.map(filePath =>
    new Promise((resolve, reject) => {
      uni.uploadFile({
        url: config.baseUrl + config.apiPrefix + '/common/upload',
        filePath,
        name: 'file',
        header: {
          'Authorization': uni.getStorageSync('token')
        },
        success: (uploadRes) => {
          try {
            const res = JSON.parse(uploadRes.data)
            resolve(res)
          } catch (e) {
            reject(new Error('上传失败'))
          }
        },
        fail: () => {
          reject(new Error('上传失败'))
        }
      })
    })
  ));
}

// 获取行政区域树
export function getRegionTree() {
  return request({
    url: '/system/region/tree',
    method: 'get'
  });
} 